import 'dart:convert';
import 'package:basic_common/common_exports.dart';
import 'qrscan_controller.dart';
import 'view/qr_scan_apppbar.dart';

class QrScanPage extends GetView<QrScanController> {
  const QrScanPage({super.key});
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          Column(
            children: [
              SizedBox(
                width: ScreenHelper.screenWidth,
                height: ScreenHelper.screenHeight,
                child: QRView(
                  key: controller.qrKey,
                  onQRViewCreated: (QRViewController qcontroller) {
                    controller.qrController = qcontroller;
                    qcontroller.scannedDataStream.listen((result) {

                      if (result.code!.isEmpty) {
                        ToastUtils.toast("扫描结果为空");
                        qcontroller.stopCamera();

                        return;
                      }
                      if (!result.code!.contains("teamNo")) {
                        ToastUtils.toast("非团队二维码，请重新扫描");
                        qcontroller.stopCamera();
                        return;
                      }
                      controller.qrResult.value = result.code!;
                      final res = json.decode(result.code!);
                      Get.toNamed(AppRoutes.applyToJoin,
                          arguments: {"teamNo": res['teamNo']});
                      qcontroller.stopCamera();
                    });
                  },
                ),
              ),
            ],
          ),
          Positioned(
            child: buildAppBar(context),
          ),
        ],
      ),
    );
  }

  Widget buildQrScanView(BuildContext context) {
    double width = MediaQuery.of(context).size.width;
    double height = MediaQuery.of(context).size.height;
    double scanW = width * 0.75;
    double scanMY = (height - scanW) / 2.0 + scanW + 15.0;
    return Container(
      alignment: Alignment.center,
      child: Stack(
        alignment: Alignment.center,
        children: [
          // ScanView(
          //   controller: controller.scanController,
          //   // custom scan area, if set to 1.0, will scan full area
          //   scanAreaScale: 0.85,
          //   scanLineColor: Colors.green.shade400,
          //   onCapture: (data) {
          //     // do something
          //     print("扫描出了结果---${data}");
          //     if (data.isNotEmpty) {
          //       Get.toNamed(AppRoutes.applyToJoin, arguments: {"teamNo": data});
          //     }else{
          //       ToastUtils.toast("扫描内容为空！");
          //     }
          //     // openQrScanWebPage(data);
          //   },
          // ),
          Positioned(
            top: scanMY,
            child: buildOption(context, scanMY),
          ),
        ],
      ),
    );
  }

  Widget buildAppBar(BuildContext context) {
    return QrScanAppBar(
      toolbarHeight: 44.0,
      backgroundColor: Colors.transparent,
      padding: const EdgeInsets.symmetric(horizontal: 10.0),
      barPadding: const EdgeInsets.symmetric(vertical: 4.0),
      leadingWidget: Container(
        alignment: Alignment.center,
        child: GestureDetector(
          onTap: () {
            Get.back();
          },
          child: Icon(
            Icons.arrow_back_ios,
            size: ScreenHelper.width(29),
            color: Colors.white,
            weight: 200,
          ),
        ),
      ),
      centerWidget: Text(
        "扫一扫",
        textAlign: TextAlign.center,
        softWrap: true,
        style: TextStyle(
          fontSize: 17,
          color: Colors.white,
          fontWeight: FontWeight.w600,
          fontStyle: FontStyle.normal,
          decoration: TextDecoration.none,
        ),
      ),
      trailingWidget: Container(
        width: 32.0,
        height: 32.0,
      ),
    );
  }

  Widget buildOption(BuildContext context, double originY) {
    return Container(
      height: ScreenHelper.screenHeight - originY,
      width: ScreenHelper.screenWidth,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Container(
            width: 300.0,
            child: Text(
              '将二维码放入扫码区,即可自动扫描',
              textAlign: TextAlign.center,
              softWrap: true,
              style: TextStyle(
                fontSize: 15,
                fontWeight: FontWeight.w500,
                fontStyle: FontStyle.normal,
                color: Colors.white,
                decoration: TextDecoration.none,
              ),
            ),
          ),
          SizedBox(
            height: 25.0,
          ),
          // buildButtons(context),
          Expanded(
            child: Container(),
          ),
        ],
      ),
    );
  }

  // Widget buildButtons(BuildContext context) {
  //   return Row(
  //     mainAxisAlignment: MainAxisAlignment.center,
  //     crossAxisAlignment: CrossAxisAlignment.center,
  //     children: [
  //       Padding(
  //         padding: EdgeInsets.symmetric(horizontal: 20.0),
  //         child: roundBtn(
  //             text: controller.torchOn.value ? "关闭灯光" : "开启灯光",
  //             textColor: Colors.white,
  //             bgColor: Colors.transparent,
  //             borderColor: Colors.white,
  //             onTap: () {
  //               controller.changedTorchMode();
  //             }),
  //         // child: QrscanButton(
  //         //   bgColor: ColorUtil.hexColor(0x35fb99),
  //         //   bgHighlightedColor: Colors.green.shade400,
  //         //   onPressed: () {
  //         //     changedTorchMode();
  //         //   },
  //         //   width: 100.0,
  //         //   height: 50.0,
  //         //   borderRadius: 25.0,
  //         //   child: Text(
  //         // (torchOn
  //         //     ? S.of(context).qrScanTorchOff
  //         //     : S.of(context).qrScanTorchOn),
  //         //     textAlign: TextAlign.center,
  //         //     softWrap: true,
  //         //     style: TextStyle(
  //         //       fontSize: 14,
  //         //       color: Colors.white,
  //         //       fontWeight: FontWeight.w600,
  //         //       fontStyle: FontStyle.normal,
  //         //       decoration: TextDecoration.none,
  //         //     ),
  //         //   ),
  //         // ),
  //       ),
  //       Padding(
  //         padding: EdgeInsets.symmetric(horizontal: 20.0),
  //         child: roundBtn(
  //             text: "重新扫描",
  //             textColor: Colors.white,
  //             bgColor: Colors.transparent,
  //             borderColor: Colors.white,
  //             onTap: () {
  //               controller.refreshScan();
  //             }),
  //       ),
  //     ],
  //   );
  // }
}
